package com.itheima.string;

public class Demo2String {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "abc";
        System.out.println("s1 == s2-->"+(s1 == s2));//true,指向常量池中相同的内存地址
        //每次new肯定都是新地址
        String s3 = new String("abc");
        String s4 = new String("abc");
        System.out.println("s3 == s4-->"+(s3 == s4)); //false,每次new都会产生新的堆内存地址
        System.out.println("s3 == s2-->"+(s3 == s2)); //false,s3是new出来在堆里,s2是双引号直接赋值指向常量池肯定不一样
        //字符串变量+常量 等同于new
        String s5 = "ab";
        String s6 = s5+"c";
        String s7 = new String("abc");
        System.out.println("s6 == s7-->"+(s6 == s7));//false.s6是变量+常量等同于new出来的在堆里,s7也是new出来,指向不同的内存地址
        System.out.println("s6 == s1-->"+(s6 == s1));//false,s6是变量+常量等同于new出来的,在堆里,s1在常量池中

        String s8 = s5+"c";
        System.out.println("s6 == s8-->"+(s6 == s8)); //false
        //常量+常量-->常量优化机制,存在于常量池
        String s9 = "ab"+"c";
        String s10 = "a"+"b"+"c";//在编译阶段会自动拼接成字符串"abc",存在于常量池中
        System.out.println("s9 == s10-->"+(s9 == s10));//true
        System.out.println("s10 == s1-->"+(s10 == s1));//true
    }
}
